home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  3.6 KB  |  176 lines  |  [TEXT/R*ch]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5. */
  6.  
  7. #define MPW3 true
  8.  
  9. #include <OSUtils.h>
  10. #include <Errors.h>
  11. #include <Files.h>
  12. #include <Resources.h>
  13. #ifdef MPW3 
  14. #include <Memory.h>
  15. #endif
  16.  
  17. #define OPENRESOLVER    1
  18. #define CLOSERESOLVER    2
  19. #define STRTOADDR        3
  20. #define    ADDRTOSTR        4
  21. #define    ENUMCACHE        5
  22. #define ADDRTONAME        6
  23.  
  24. Handle codeHndl = nil;
  25.  
  26. typedef OSErr (*OSErrProcPtr)();
  27. OSErrProcPtr dnr = nil;
  28.  
  29.  
  30. /* OpenOurRF is called to open the MacTCP driver resources */
  31.  
  32. short OpenOurRF() {
  33.     SysEnvRec info;
  34.     HParamBlockRec fi;
  35.     Str255 filename;
  36.     
  37.     SysEnvirons(1, &info);
  38.     
  39.     fi.fileParam.ioCompletion    = nil;
  40.     fi.fileParam.ioNamePtr        = (StringPtr)&filename;
  41.     fi.fileParam.ioVRefNum        = info.sysVRefNum;
  42.     fi.fileParam.ioDirID        = 0;
  43.     fi.fileParam.ioFDirIndex    = 1;
  44.     
  45.     while (PBHGetFInfoSync(&fi) == noErr) {
  46.         /* scan system folder for driver resource files of specific type & creator */
  47.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' && fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp')
  48.             {
  49.             /* found the MacTCP driver file */
  50.             ParamBlockRec    pb;
  51.             pb.volumeParam.ioNamePtr = 0;
  52.             pb.volumeParam.ioVRefNum = info.sysVRefNum;
  53.             PBSetVolSync(&pb);
  54.             return(OpenResFile( (const unsigned char *)&filename));
  55.             }
  56.         
  57.         /* check next file in system folder */
  58.         fi.fileParam.ioFDirIndex++;
  59.         fi.fileParam.ioDirID = 0;
  60.     }
  61.     return(-1);
  62. }
  63.  
  64.  
  65.  
  66. OSErr OpenResolver(fileName)
  67. char *fileName;
  68. {
  69.     short refnum;
  70.     OSErr rc;
  71.     
  72.     if (dnr != nil)
  73.         /* resolver already loaded in */
  74.         return(noErr);
  75.         
  76.     /* open the MacTCP driver to get DNR resources. Search for it based on
  77.        creator & type rather than simply file name */    
  78.     refnum = OpenOurRF();
  79.  
  80.     /* ignore failures since the resource may have been installed in the 
  81.        System file if running on a Mac 512Ke */
  82.        
  83.     /* load in the DNR resource package */
  84.     codeHndl = GetIndResource('dnrp', 1);
  85.     if (codeHndl == nil) {
  86.         /* can't open DNR */
  87.         return(ResError());
  88.         }
  89.     
  90.     DetachResource(codeHndl);
  91.     if (refnum != -1) {
  92.         CloseResFile(refnum);
  93.         }
  94.         
  95.     /* lock the DNR resource since it cannot be reloated while opened */
  96.     HLock(codeHndl);
  97.     dnr = (OSErrProcPtr) *codeHndl;
  98.     
  99.     /* call open resolver */
  100.     rc = (*dnr)(OPENRESOLVER, fileName);
  101.     if (rc != noErr) {
  102.         /* problem with open resolver, flush it */
  103.         HUnlock(codeHndl);
  104.         DisposHandle(codeHndl);
  105.         dnr = nil;
  106.         }
  107.     return(rc);
  108.     }
  109.  
  110.  
  111. OSErr CloseResolver()
  112. {
  113.     if (dnr == nil)
  114.         /* resolver not loaded error */
  115.         return(notOpenErr);
  116.         
  117.     /* call close resolver */
  118.     (void) (*dnr)(CLOSERESOLVER);
  119.  
  120.     /* release the DNR resource package */
  121.     HUnlock(codeHndl);
  122.     DisposHandle(codeHndl);
  123.     dnr = nil;
  124.     return(noErr);
  125.     }
  126.  
  127. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  128. char *hostName;
  129. struct hostInfo *rtnStruct;
  130. long resultproc;
  131. char *userDataPtr;
  132. {
  133.     if (dnr == nil)
  134.         /* resolver not loaded error */
  135.         return(notOpenErr);
  136.         
  137.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  138.     }
  139.     
  140. OSErr AddrToStr(addr, addrStr)
  141. unsigned long addr;
  142. char *addrStr;                                    
  143. {
  144.     if (dnr == nil)
  145.         /* resolver not loaded error */
  146.         return(notOpenErr);
  147.         
  148.     (*dnr)(ADDRTOSTR, addr, addrStr);
  149.     return(noErr);
  150.     }
  151.     
  152. OSErr EnumCache(resultproc, userDataPtr)
  153. long resultproc;
  154. char *userDataPtr;
  155. {
  156.     if (dnr == nil)
  157.         /* resolver not loaded error */
  158.         return(notOpenErr);
  159.         
  160.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  161.     }
  162.     
  163.     
  164. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  165. unsigned long addr;
  166. struct hostInfo *rtnStruct;
  167. long resultproc;
  168. char *userDataPtr;                                    
  169. {
  170.     if (dnr == nil)
  171.         /* resolver not loaded error */
  172.         return(notOpenErr);
  173.         
  174.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  175.     }
  176.